//
//  VipUserInfoObject.m
//  HappyPenguin
//
//  Created by zhuangyihang on 1/25/16.
//  Copyright © 2016 zhuangyihang. All rights reserved.
//

#import "VipUserInfoObject.h"
#import "AppHelper.h"

NSString *DateCalendarTypeValue[] = {
    [DateCalendarType_International] = @"公历",
    [DateCalendarType_Traditional] = @"农历",
};

NSDictionary* DateCalendarTypeDictionary(){
    return @{
             DateCalendarTypeValue[DateCalendarType_International]:@(DateCalendarType_International),
             DateCalendarTypeValue[DateCalendarType_Traditional]:@(DateCalendarType_Traditional),
             };
}

@implementation VipUserInfoObject

+ (NSDictionary *)JSONKeyPathsByPropertyKey{
    return @{
        @"attributes":@"VIPINFO",
        @"anniversaries":@"MEMORIALDAYDAY",
    };
}

+(NSValueTransformer *)attributesJSONTransformer{
    return [BaseObject objectArrayJSONTransformer:VipUserInfoFieldObject.class];;
}

+(NSValueTransformer *)anniversariesJSONTransformer{
    return [BaseObject objectArrayJSONTransformer:VipUserInfoAnniversaryFieldObject.class];;
}

- (NSString *)vipName{
    for ( VipUserInfoFieldObject *obj in self.attributes ) {
        if ([obj.field isEqualToString:@"C_NAME"]) {
            return obj.value;
        }
    }
    return @"未知";
}
- (NSString *)vipGender{
    for ( VipUserInfoFieldObject *obj in self.attributes ) {
        if ([obj.field isEqualToString:@"C_SEX"]) {
            return obj.value;
        }
    }
    return @"未知";
}

- (NSString *)vipPhone{
    for ( VipUserInfoFieldObject *obj in self.attributes ) {
        if ([obj.field isEqualToString:@"C_SEX"]) {
            return obj.value;
        }
    }
    return @"未知";
}

- (NSString *)vipPortrait{
    for ( VipUserInfoFieldObject *obj in self.attributes ) {
        if ([obj.field isEqualToString:@"IMAGE"]) {
            return [AppHelper getImagePath:obj.value type:PictureType_Vip].absoluteString;
        }
    }
    return @"";
}

- (NSString *)vipTotalMoney{
    for ( VipUserInfoFieldObject *obj in self.attributes ) {
        if ([obj.field isEqualToString:@"TOTALMONEY"]) {
            return obj.value;
        }
    }
    return @"0";
}
- (NSString *)vipTotalCount{
    for ( VipUserInfoFieldObject *obj in self.attributes ) {
        if ([obj.field isEqualToString:@"TOTALCOUNT"]) {
            return obj.value;
        }
    }
    return @"0";
}
- (NSString *)vipLastBuy{
    for ( VipUserInfoFieldObject *obj in self.attributes ) {
        if ([obj.field isEqualToString:@"D_LASTBUY"]) {
            return obj.value;
        }
    }
    return @"未知";
}

- (NSArray *)getUserAttributes{
    NSArray *specialKey = @[@"C_SEX",@"C_NAME",@"TOTALMONEY",@"TOTALCOUNT",@"D_LASTBUY",@"IMAGE"];
    NSMutableArray *array = [NSMutableArray array];
    for ( VipUserInfoFieldObject *obj in self.attributes ) {
        BOOL shouldSkip = NO;
        for (NSString *key in specialKey) {
            if ([obj.field isEqualToString:key]) {
                shouldSkip = YES;
                break;
            }
        }
        
        if (!shouldSkip) {
            [array addObject:obj];
        }
    }
    
    return array;
}

@end

@implementation VipUserInfoFieldObject

+ (NSDictionary *)JSONKeyPathsByPropertyKey{
    return @{
             @"value":@"VALUE",
             @"field":@"FIELD",
             @"name":@"NAME",
             };
}

+(NSValueTransformer *)valueJSONTransformer{
    return [MTLValueTransformer transformerUsingForwardBlock:^id(id val, BOOL *success, NSError *__autoreleasing *error){
        if ([val isKindOfClass:[NSNumber class]]) {
            NSNumber *n = val;
            return [n stringValue];
        }else{
            return val;
        }
    }];
}

@end

@implementation VipUserInfoAnniversaryFieldObject

+ (NSDictionary *)JSONKeyPathsByPropertyKey{
    return @{
             @"name":@"NAME",
             @"type":@"BIRTHDAYTYPE",
             @"date":@"DATE",
             @"keyId":@"KEY_ID",
             };
}

+(NSValueTransformer *)typeJSONTransformer{
    return [NSValueTransformer mtl_valueMappingTransformerWithDictionary:DateCalendarTypeDictionary()];
}

+(NSValueTransformer *)keyIdJSONTransformer{
    return [BaseObject numberJSONTransformer];
}

+(NSValueTransformer *)dateJSONTransformer{
    return [BaseObject dateStringJSONTransformer];
}
@end